aboutsummaryrefslogtreecommitdiff
path: root/pages/en/manga/[id].js
blob: 5e46599a5d12a255d5ef56a92530161a07917e98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import dotenv from "dotenv";
import ChapterSelector from "../../../components/manga/chapters";
import HamburgerMenu from "../../../components/manga/mobile/hamburgerMenu";
import Navbar from "../../../components/navbar";
import TopSection from "../../../components/manga/info/topSection";
import Footer from "../../../components/footer";
import Head from "next/head";
import { useEffect, useState } from "react";
import { setCookie } from "nookies";
import { getServerSession } from "next-auth";
import { authOptions } from "../../api/auth/[...nextauth]";

export default function Manga({ info, userManga, chapters }) {
  const [domainUrl, setDomainUrl] = useState("");
  const [firstEp, setFirstEp] = useState();
  const chaptersData =
    info.chapters.data.length === 0 ? chapters : info.chapters.data;

  useEffect(() => {
    setDomainUrl(window.location.origin);
  }, []);

  return (
    <>
      <Head>
        <title>
          {info
            ? `Manga - ${
                info.title.romaji || info.title.english || info.title.native
              }`
            : "Getting Info..."}
        </title>
        <meta name="twitter:card" content="summary_large_image" />
        <meta
          name="twitter:title"
          content={`Moopa - ${info.title.romaji || info.title.english}`}
        />
        <meta
          name="twitter:description"
          content={`${info.description?.slice(0, 180)}...`}
        />
        <meta
          name="twitter:image"
          content={`${domainUrl}/api/og?title=${
            info.title.romaji || info.title.english
          }&image=${info.bannerImage || info.coverImage}`}
        />
      </Head>
      <div className="min-h-screen w-screen flex flex-col items-center relative">
        <HamburgerMenu />
        <Navbar className="absolute top-0 w-full z-40" />
        <div className="flex flex-col w-screen items-center gap-5 md:gap-10 py-10 pt-nav">
          <div className="flex-center w-full relative z-30">
            <TopSection info={info} firstEp={firstEp} setCookie={setCookie} />
            <>
              <div className="absolute hidden md:block z-20 bottom-0 h-1/2 w-full bg-secondary" />
              <div className="absolute hidden md:block z-20 top-0 h-1/2 w-full bg-transparent" />
            </>
          </div>
          <div className="w-[90%] xl:w-[70%] min-h-[35vh] z-40">
            {chaptersData.length > 0 ? (
              <ChapterSelector
                chaptersData={chaptersData}
                data={info}
                setFirstEp={setFirstEp}
                setCookie={setCookie}
                userManga={userManga}
              />
            ) : (
              <p>No Chapter Available :(</p>
            )}
          </div>
        </div>
        <Footer />
      </div>
    </>
  );
}

export async function getServerSideProps(context) {
  dotenv.config();

  const session = await getServerSession(context.req, context.res, authOptions);

  const { id } = context.query;
  const key = process.env.API_KEY;
  const res = await fetch(`https://api.anify.tv/info/${id}?apikey=${key}`);
  const data = await res.json();

  let userManga = null;

  if (session) {
    const response = await fetch("https://graphql.anilist.co/", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        query: `
        query ($username: String, $status: MediaListStatus) {
    MediaListCollection(userName: $username, type: MANGA, status: $status, sort: SCORE_DESC) {
      user {
        id
        name
      }
      lists {
        status
        name
        entries {
          id
          mediaId
          status
          progress
          score
          progressVolumes
          media {
            id
            status
            title {
              english
              romaji
            }
            episodes
            coverImage {
              large
            }
          }
        }
      }
    }
  }
        `,
        variables: {
          username: session?.user?.name,
        },
      }),
    });
    const data = await response.json();
    const user = data?.data?.MediaListCollection;
    const userListsCurrent = user?.lists.find((X) => X.status === "CURRENT");
    const matched = userListsCurrent?.entries.find(
      (x) => x.mediaId === parseInt(id)
    );
    if (matched) {
      userManga = matched;
    }
  }

  if (!data?.chapters) {
    return {
      notFound: true,
    };
  }

  let chapter = null;

  if (data?.chapters?.data.length === 0) {
    const res2 = await fetch(
      `https://api.anify.tv/chapters/${id}?apikey=${key}`
    );
    const data2 = await res2.json();
    chapter = data2;
  }

  return {
    props: {
      info: data,
      userManga,
      chapters: chapter || null,
    },
  };
}